package com.yuan.sys.modular.mould.controller;

import com.yuan.core.annotion.BusinessLog;
import com.yuan.core.annotion.Permission;
import com.yuan.core.enums.LogAnnotionOpTypeEnum;
import com.yuan.core.pojo.page.PageResult;
import com.yuan.core.pojo.response.ResponseData;
import com.yuan.core.pojo.response.SuccessResponseData;
import com.yuan.sys.modular.driver.param.SysDriverParam;
import com.yuan.sys.modular.mould.param.SysMouldParam;
import com.yuan.sys.modular.mould.result.SysMouldResult;
import com.yuan.sys.modular.mould.service.SysMouldService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2021年10月24日 21:16
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class SysMouldController {

    private final SysMouldService sysMouldService;

    /**
     * 模具页面
     */
    @Permission
    @GetMapping("/sysMould/index")
    public String index() {
        return "system/sysMould/index.html";
    }

    /**
     * 模具表单页面
     */
    @GetMapping("/sysMould/form")
    public String form() {
        return "system/sysMould/form.html";
    }

    /**
     * 批量导入模具
     */
    @GetMapping("/sysMould/importForm")
    public String importForm() {
        return "system/sysMould/importForm.html";
    }

    /**
     * 查询模具
     */
    @Permission
    @ResponseBody
    @GetMapping("/sysMould/page")
    @BusinessLog(title = "模具_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public PageResult<SysMouldResult> page(SysMouldParam sysMouldParam) {
        return sysMouldService.page(sysMouldParam);
    }

    /**
     * 导出模具
     */
    @Permission
    @ResponseBody
    @GetMapping("/sysMould/export")
    @BusinessLog(title = "模具_导出", opType = LogAnnotionOpTypeEnum.EXPORT)
    public void export(SysMouldParam sysMouldParam) {
        sysMouldService.export(sysMouldParam);
    }

    /**
     * 删除模具
     */
    @Permission
    @ResponseBody
    @PostMapping("/sysMould/delete")
    @BusinessLog(title = "模具_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(SysMouldParam.delete.class) List<SysMouldParam> sysMouldParamList) {
        sysMouldService.delete(sysMouldParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑模具
     */
    @Permission
    @ResponseBody
    @PostMapping("/sysMould/edit")
    @BusinessLog(title = "模具_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(SysMouldParam.edit.class) SysMouldParam sysMouldParam) {
        sysMouldService.edit(sysMouldParam);
        return new SuccessResponseData();
    }

    /**
     * 增加模具
     */
    @Permission
    @ResponseBody
    @PostMapping("/sysMould/add")
    @BusinessLog(title = "模具_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(SysMouldParam.add.class) SysMouldParam sysMouldParam) {
        sysMouldService.add(sysMouldParam);
        return new SuccessResponseData();
    }

    /**
     * 增加模具
     */
    @Permission
    @ResponseBody
    @PostMapping("/sysMould/uploadExcel")
    @BusinessLog(title = "模具_批量导入", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData uploadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String customer = request.getParameter("customer");
        String fileName = file.getOriginalFilename();
        if (!fileName.matches("^.+\\.(?i)(xlsx)$") && !fileName.matches("^.+\\.(?i)(xls)$")) {
            return ResponseData.error("上传文件类型错误!");
        }
        try {
            sysMouldService.dealExcelData(file, customer);
            return new SuccessResponseData();
        } catch (Exception e) {
            log.error("批量导入模具异常，异常为：{}", e.getMessage());
            return ResponseData.error("批量导入模具异常!");
        }

    }


}
